//
//  HWTakeALookDetailCommentFlowLayout.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/4.
//

import UIKit

class HWTakeALookDetailCommentFlowLayout: UICollectionViewFlowLayout {
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        let attrs = super.layoutAttributesForElements(in: rect)
      
        if attrs?.count ?? 0 > 0 {

            for i in 1..<attrs!.count {
                let currentLayoutAttributes = attrs?[i]
                
                let oldLayoutAttributes = attrs?[i-1]
                if currentLayoutAttributes?.indexPath.section == 0 {
                    
                    let layoutAttributeOne = attrs?[0]
                    var oneFrame = layoutAttributeOne?.frame
                    oneFrame?.origin.x = 15
                    layoutAttributeOne?.frame = oneFrame ?? CGRect.zero
                    
                    if currentLayoutAttributes?.representedElementKind == UICollectionView.elementKindSectionHeader || currentLayoutAttributes?.representedElementKind == UICollectionView.elementKindSectionFooter {

                    }else{
                        if currentLayoutAttributes?.indexPath.row  == 0 {
                            
                            var frame = currentLayoutAttributes?.frame
                            frame!.origin.x = 15
                            currentLayoutAttributes?.frame = frame!
                        }else{
                            if (currentLayoutAttributes?.indexPath.row ?? 0) % 3 == 0 {
                                var frame = currentLayoutAttributes?.frame
                                frame!.origin.x = 15
                                frame!.origin.y = (oldLayoutAttributes?.frame.maxY)!+5
                                currentLayoutAttributes?.frame = frame!
                            }else{
                                var frame = currentLayoutAttributes?.frame
                                frame!.origin.x = (oldLayoutAttributes?.frame.maxX)! + 5
                                frame!.origin.y = oldLayoutAttributes?.frame.minY ?? 0
                                currentLayoutAttributes?.frame = frame!
                            }

                        }

                    }
                }
                

            }
        }
        
        return attrs
    }
}
